use rusqlite::{Connection, Result};

///https://www.sqlite.org/download.html 下载sqlite3的dll动态库 拷贝到 mingw 的lib目录

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>,
}


pub fn test_sqlite()->Result<()>{
    // let conn = Connection::open_in_memory()?;
    let db_file = "person.db";
    let conn = Connection::open(db_file)?;
    //创建表
    conn.execute(
        "CREATE TABLE person (
            id    INTEGER PRIMARY KEY,
            name  TEXT NOT NULL,
            data  BLOB
        )",
        (), // empty list of parameters.
    )?;
    let me = Person {
        id: 0,
        name: "Steven".to_string(),
        data: None,
    };
    //添加数据
    conn.execute(
        "INSERT INTO person (name, data) VALUES (?1, ?2)",
        (&me.name, &me.data),
    )?;

    //查询
    let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
    let maped_rows = stmt.query_map([], |row| {
        let u = Person{
            id: row.get(0)?,
            name: row.get(1)?,
            data: row.get(2)?,
        };
        Ok(u)
    })?;


    for person in maped_rows {
        println!("Found person {:?}", person.unwrap());
    }
    return Ok(())
    
}